package com.xiao.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author lao xiao
 * @date 2025-06-15 10:53
 */
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/getPort")
    public String getPort() {
        return restTemplate.getForObject("http://SERVER-8001/test/getPort", String.class);
    }


    @HystrixCommand(commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")
    }, fallbackMethod = "getPortTimeOutFallback")
    @GetMapping("/getPortTimeOut")
    public String getPortTimeOut() {
        return restTemplate.getForObject("http://SERVER-8001/test/getPortTimeOut", String.class);
    }

    public String getPortTimeOutFallback() {
        return "-1";
    }


    @HystrixCommand(threadPoolKey = "timeOutA", threadPoolProperties = {
            @HystrixProperty(name = "coreSize", value = "1"),
            @HystrixProperty(name = "maxQueueSize", value = "10"),
    })
    @GetMapping("/A")
    public String A() {
        return restTemplate.getForObject("http://SERVER-8001/test/getPortTimeOut", String.class);
    }

    @HystrixCommand(threadPoolKey = "timeOutA", threadPoolProperties = {
            @HystrixProperty(name = "coreSize", value = "1"),
            @HystrixProperty(name = "maxQueueSize", value = "10"),
    })
    @GetMapping("/B")
    public String B() {
        return restTemplate.getForObject("http://SERVER-8001/test/getPortTimeOut", String.class);
    }
}
